#!/bin/bash

# author Matej Isteník <matej.istenik@gmail.com>

WIDTH=0
HEIGHT=0

LEFT_LIST_COUNT=$2
RIGHT_LIST_COUNT=$4

LEFT_LIST=()
RIGHT_LIST=()

SHOW_HELP=$6

SELECTED=$1
SELECTED_LEFT_INDEX=$3
SELECTED_RIGHT_INDEX=$5

WORKING_DIR_LEFT=$7
WORKING_DIR_RIGHT=$8

# $1 left / right
# $2 count
function loadFiles
{
	local LIST
	local TYPE
	local NAME
	local DATE
	local SIZE
	
	for index in `seq 0 ${2}`
	do
		read TYPE NAME DATE SIZE

		LIST[${index}]="${TYPE} ${NAME} ${DATE} ${SIZE}"
	done
	
	case ${1} in
	"left")
		LEFT_LIST=(${LIST[@]})
	;;
	"right")
		RIGHT_LIST=(${LIST[@]})
	;;
	*)
		echo "loadFiles bad parameter"
		exit
	esac
}

loadFiles left $LEFT_LIST_COUNT
loadFiles right $RIGHT_LIST_COUNT

function drawEnvironment
{
	let WIDTH=$(tput cols)
	let HEIGHT=$(tput lines)
	
	drawHeader
	
	drawCenterLine
	
	if [ $SHOW_HELP = "1" ]
	then
		drawHelp
	fi
	
	drawPanel left
	drawPanel right
	
	moveCursor ${HEIGHT} 0
	
}

function drawHeader
{
	moveCursor 0 0
	setFontStyle title
	
	local TITLE="3:x) EvilKitty v3.1416" # v0.1^sin(2*PI))"
	local TITLE_SIZE=${#TITLE}
	let TITLE_SIZE+=2
	
	local AUTHORS="(c) whileTrueDrink 2011"
	local AUTHORS_SIZE=${#AUTHORS}
	let AUTHORS_SIZE+=2

	local CENTER
	let CENTER="$WIDTH / 2"
	
	local LEFT_BORDER
	local RIGHT_BORDER
	
	let LEFT_BORDER="(${WIDTH} / 2 - ${TITLE_SIZE}) - 4"
	let RIGHT_BORDER="(${WIDTH} / 2 - ${AUTHORS_SIZE}) - 3"
	
	local LEFT_OFFSET
	local RIGHT_OFFSET
	
	let LEFT_OFFSET="${#WORKING_DIR_LEFT} + 3"
	let RIGHT_OFFSET="${#WORKING_DIR_RIGHT} + 4"
	
	local EVEN
	let EVEN="$WIDTH % 2"
	if [ $EVEN -eq 1 ]
	then
		let RIGHT_BORDER+=1
	fi
	
	
	echo -n "X $WORKING_DIR_LEFT "
	
	for i in `seq ${LEFT_OFFSET} ${LEFT_BORDER}`
	do
		printf X
	done
	
	printf X
	echo -n " $TITLE "
	printf X
	printf "|"
	printf X
	echo -n " $AUTHORS "
	printf X
	
	for i in `seq ${RIGHT_OFFSET} ${RIGHT_BORDER}`
	do
		printf X
	done
	
	echo -n " $WORKING_DIR_RIGHT X"
	
	echo
	
}

function drawCenterLine
{
	local CENTER
	let CENTER="${WIDTH} / 2"	
	
	local LINE_HEIGHT
	let LINE_HEIGHT="${HEIGHT} - 1 - ${SHOW_HELP}"
	
	for index in `seq 2 ${LINE_HEIGHT}`
	do
		moveCursor $index $CENTER
		printf "|"
	done
}

function drawHelp
{
	local POSITION
	let POSITION="${HEIGHT}-1"
	
	moveCursor $POSITION 0
	
	local KEYS=(^H F2 F3 F4 F5 F6 F7 F8 F9)
	local ACTIONS=(help rename show edit copy move mkdir delete refresh)
	
	for index in `seq 0 8`
	do
		setFontStyle listCursor
		echo -n "${KEYS[${index}]}"
		setFontStyle listFile
		echo -n " ${ACTIONS[${index}]} "
	done
}

# $1 left / right
function drawPanel
{
	local LIST
	local LIST_COUNT
	
	local SIDE=${1}

	local PANEL_WIDTH
	let PANEL_WIDTH=${WIDTH}/2

	local PANEL_HEIGHT
	let PANEL_HEIGHT="${HEIGHT} - 3 - ${SHOW_HELP}"

	local COLUMN_OFFSET
	local DATE_OFFSET=0
	local SIZE_OFFSET=0
	
	local SELECTED_INDEX
	
	case $SIDE in
	"left")
		LIST=(${LEFT_LIST[@]})
		LIST_COUNT=${LEFT_LIST_COUNT}
		SELECTED_INDEX=$SELECTED_LEFT_INDEX
		COLUMN_OFFSET=0
	;;
	"right")
		LIST=(${RIGHT_LIST[@]})
		LIST_COUNT=${RIGHT_LIST_COUNT}
		SELECTED_INDEX=$SELECTED_RIGHT_INDEX
		COLUMN_OFFSET=$PANEL_WIDTH
		let COLUMN_OFFSET+=1
	;;
	*)
		echo "drawList bad parameter"
		exit
	esac

	let DATE_OFFSET="(${PANEL_WIDTH} -24) + COLUMN_OFFSET"
	
	let SIZE_OFFSET="(${PANEL_WIDTH} -10) + COLUMN_OFFSET"
	
	local ITEMS_OFFSET="${SELECTED_LEFT_INDEX} + ${PANEL_HEIGHT} / 2"
	ITEMS_OFFSET=0
	
	local START
	local END
	
	if [ $LIST_COUNT -lt $PANEL_HEIGHT ]
	then
		let START=0
		let END="${LIST_COUNT} - 1"
	else
		let START="${SELECTED_INDEX} - ${PANEL_HEIGHT} / 2"
	
		if [ $START -lt 0 ]
		then
			START=0
		fi
	
		let END="${START} + ${PANEL_HEIGHT}"

		if [ $END -gt $LIST_COUNT ]
		then
			let END="${LIST_COUNT} - 1"
			let START="${LIST_COUNT} - ${PANEL_HEIGHT} - 1"
		fi
	fi
	
	if [ $SELECTED_INDEX -lt 0 ]
	then
		SELECTED_INDEX=0
	fi
	
	if ! [ $SELECTED_INDEX -lt $LIST_COUNT ]
	then
		let SELECTED_INDEX="${LIST_COUNT} - 1"
	fi

	for index in `seq ${START} ${END}`
	do
		local ITEM_POS
		let ITEM_POS="${index} + 2 - ${START}"
		
		local ITEM_INDEX=${index}
		
		let ITEM_INDEX=${ITEM_INDEX}*4
		
		local TYPE=${LIST[${ITEM_INDEX}]}
		
		if [ ! ${TYPE} ]
		then
			break
		fi
		
		let ITEM_INDEX+=1
		local NAME=${LIST[${ITEM_INDEX}]}
		NAME="${NAME//:;/ }"
		
		let ITEM_INDEX+=1
		local DATE=${LIST[${ITEM_INDEX}]}
		
		let ITEM_INDEX+=1
		local SIZE=${LIST[${ITEM_INDEX}]}
		
		if [ $index -eq ${SELECTED_INDEX} ]
		then
			if [ $index -eq ${SELECTED_INDEX} ] && [ ${SIDE} = ${SELECTED} ]
			then
				drawCursor
			else
				setFontStyle listSelected
			fi
		else
			if [ $TYPE = "d" ]
			then
				setFontStyle listDir
			else
				setFontStyle listFile
			fi
		fi

		printName $TYPE "$NAME"
		printDate $DATE
		printSize $SIZE
		
		tput sgr0
	done
}

function drawCursor
{
	moveCursor $ITEM_POS $COLUMN_OFFSET
	
	setFontStyle listCursor

	local CURSOR_WIDTH
	let CURSOR_WIDTH="${PANEL_WIDTH} - 2"

	for i in `seq 0 ${CURSOR_WIDTH}`
	do
		echo -n " "
	done
}

function printName
{
	moveCursor $ITEM_POS $COLUMN_OFFSET
	echo -n " ${2}"
}

function printDate
{
	moveCursor $ITEM_POS $DATE_OFFSET
	
	echo $(date -d "1970-01-01 $1 sec" "+ %d.%m. %H:%M")
}

function printSize
{
	moveCursor $ITEM_POS $SIZE_OFFSET
	
	echo $1
}

# $1 height
# $2 width
function moveCursor
{
	echo -en "\033[${1};${2}f"
}

# $1 predefined style
function setFontStyle
{
	case ${1} in
	"title")
		echo -en "\e[01;33m"
	;;
	"listDir")
		echo -en "\e[01;36m"
	;;
	"listFile")
		echo -en "\e[00;33m"
	;;
	"listSelected")
		echo -en "\e[01;33m"
	;;
	"listCursor")
		echo -en "\e[01;33;44m"
	;;
	*)
		echo "setFontColor bad parameter"
		exit
	esac
}

# rozhranie

function setFilesInPanel
{
	clear
	# $1 panel (left, right)
	# $2 pole stringov (ak ho dokazes poslat)
}

function setActiveFile
{
	clear
	# $1 panel (left, right)
	# $2 index
	# logiku ci sa scrollne alebo nescrollne je na logike funkcie
}

clear
drawEnvironment
